<template>
  <div class="dashboard-container">
    <div>
      <div>
        <el-row>
          <el-col :span="2">
            <div style="margin-top: 8px" class="grid-content bg-purple-light">课程分类：</div>
          </el-col>
          <el-col :span="22">
            <div class="grid-content bg-purple-dark">
              <el-select @change="queryTwoLevelClass(ruleForm.teacher_parentid)" v-model="ruleForm.teacher_parentid"
                         filterable
                         placeholder="一级分类">
                <el-option v-for="item in oneClass" :key="item.id" :label="item.cat_name" :value="item.id">
                </el-option>
              </el-select>
              <el-select @change="queryThreeLevelClass(ruleForm.teacher_secondid)" v-model="ruleForm.teacher_secondid"
                         filterable placeholder="二级分类">
                <el-option v-for="item in twoClass" :key="item.id" :label="item.cat_name" :value="item.id">
                </el-option>
              </el-select>
              <el-select v-model="ruleForm.teacher_catid" filterable placeholder="三级分类">
                <el-option v-for="item in threeClass" :key="item.id" :label="item.cat_name" :value="item.id">
                </el-option>
              </el-select>
            </div>
          </el-col>
        </el-row>
      </div>

      <el-row>
        <el-col :span="12">
          <div style="margin-top: 20px">
            <span class="label_style">状态：</span>
            <el-select v-model="ruleForm.teachers_status" placeholder="请选择">
              <el-option v-for="item in status" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </div>
        </el-col>
        <el-col :span="10">
          <div style="margin-top: 20px">
            <div class="grid-content bg-purple-light">
              <el-col :span="6">
                <div style="margin-top: 8px" class="grid-content bg-purple-light">课程分类：</div>
              </el-col>
              <el-col :span="15">
                <el-input v-model="ruleForm.teacher_name" placeholder="请输入校区名称"></el-input>
              </el-col>
            </div>
          </div>
        </el-col>
      </el-row>


      <div style="margin-top: 15px;display: flex;">
        <div class="el-select" style="width: 500px;">
          <el-button type="primary" class="div-button" @click="search">搜索</el-button>
          <el-button type="primary" class="div-button" @click="reset">重置</el-button>
        </div>
      </div>
      <el-row style="margin-top: 15px;">
        <el-button type="primary" @click="$router.push({path: '/teacherDetail', query: {id: $route.query.id}})" style="padding: 12px 40px;">添加
        </el-button>
      </el-row>
    </div>
    <div>
      <el-main style="margin-top: 15px;padding: 0;">
        <el-table ref="menuTable"
                  style="width: 100%"
                  :data="list"
                  border>
          <el-table-column label="id" align="center">
            <template slot-scope="scope">{{ scope.row.id }}</template>
          </el-table-column>
          <el-table-column label="校区名称" align="center">
            <template slot-scope="scope">{{ scope.row.teacher_name }}</template>
          </el-table-column>
          <el-table-column label="机构名称" align="center">
            <template slot-scope="scope">{{ scope.row.school_name }}</template>
          </el-table-column>
          <el-table-column label="排序" align="center">
            <!--<template slot-scope="scope">{{ scope.row.cat_listorder }}</template>-->
            <template slot-scope="scope">
              <el-form :model="scope.row">
                <el-form-item size="mini">
                  <el-input v-enter-number v-model="scope.row.teacher_orderid" @blur="changeNum(scope.$index, scope.row)"></el-input>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column label="是否开启" align="center">
            <template slot-scope="scope">
              <el-switch
                @change="handleHiddenChange(scope.$index, scope.row)"
                :active-value="1"
                :inactive-value="0"
                v-model="scope.row.teachers_status">
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="handleUpdate(scope.$index, scope.row)">编辑
              </el-button>
              <el-button
                size="mini"
                type="text"
                @click="handleDelete(scope.$index, scope.row)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
    </div>
    <div class="block block-div">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page.sync="ruleForm.page"
                     :page-sizes="[10, 20, 30, 40, 50]" :page-size="ruleForm.page_size"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {
  teacherDel,
  cateComList,
  cityComList,
  campusList,
  courseList,
  courseAjax,
  campusAjax,
  campusDel,
  teacherList,
  teacherAjax
} from "@/api/menu";
import {mapGetters} from "vuex";

export default {
  data() {
    return {
      ruleForm: {
        teacher_name: '',
        teacher_parentid: '',
        teacher_secondid: '',
        teacher_catid: '',
        teachers_status: '',
        page: 1,
        page_size: 10,
      },
      list: [],
      total: null,
      status: [{
        value: '0',
        label: '未开启'
      }, {
        value: '1',
        label: '开启'
      }],
      oneClass: [],
      twoClass: [],
      threeClass: [],
      oneLevel: [],
      twoLevel: [],
      threeLevel: [],
      value: '',
      input: '',
      currentPage4: 1,
    }
  },
  computed: {
    ...mapGetters([
      'schoolId'
    ]),
  },
  mounted() {
    /*  this.getOneLevel()*/
    this.getOneLevelClass()
    this.getTeacherList()
  },
  methods: {
    /**
     * 获取列表单条信息
     * @param index
     * @param row
     */
    handleUpdate(index, row) {
      this.$router.push({path: '/teacherUpdate', query: {rowId: row.id,id: this.$route.query.id}});
      console.log(row)
    },
    /**
     * 排序
     * @param index
     * @param row
     */
    changeNum(index, row) {
      teacherAjax({id: row.id, field: "teacher_orderid", value: row.teacher_orderid}).then(response => {
        this.$message({
          message: '修改成功',
          type: 'success',
          duration: 1000
        });
      });
    },
    /**
     * 是否在前台显示
     * @param index
     * @param row
     */
    handleHiddenChange(index, row) {
      teacherAjax({id: row.id, field: "teacher_status", value: row.teachers_status}).then(response => {
        this.$message({
          message: '修改成功',
          type: 'success',
          duration: 1000
        });
      });
    },
    /**
     * 重置
     */
    reset() {
      this.ruleForm = {
        teacher_name: '',
        teacher_parentid: '',
        teacher_secondid: '',
        teacher_catid: '',
        teachers_status: '',
        page: 1,
        page_size: 10,
      };
      this.getTeacherList();
    },
    /**
     * 搜索
     */
    search() {
      this.getTeacherList(this.ruleForm)
    },
    /**
     * 列表
     */
    getTeacherList() {
      teacherList(Object.assign(this.ruleForm, {teacher_schoolid:this.schoolId ? this.schoolId :this.$route.query.id})).then(response => {
        console.log(response);
        this.list = response.data.data_list;
        this.total = response.data.total;
      }).catch((e) => {
      })
    },
    /**
     * 市
     */
    queryTwoLevel(value) {
      console.log(value);
      this.ruleForm.campus_cityid = '';
      this.ruleForm.campus_areaid = '';
      cityComList({
        city_provinceid: value,
        city_cityid: 0
      }).then(response => {
        console.log(response);
        this.twoLevel = response.data
      })
    },
    /**
     * 区
     */
    queryThreeLevel(value) {
      this.ruleForm.campus_areaid = '';
      console.log(value);
      cityComList({
        city_provinceid: value,
        city_cityid: this.ruleForm.campus_provinceid
      }).then(response => {
        console.log(response);
        this.threeLevel = response.data
      })
    },
    /**
     * 省
     */
    getOneLevel() {
      cityComList({
        city_provinceid: 0,
        city_cityid: 0
      }).then(response => {
        console.log(response);
        this.oneLevel = response.data
      })
    },
    /**
     * 查询二级数据
     */
    queryTwoLevelClass(value) {
      console.log(value);
      this.ruleForm.teacher_secondid = '';
      this.ruleForm.teacher_catid = '';
      cateComList({
        cate_parentid: value,
        cate_secondid: 0
      }).then(response => {
        console.log(response);
        this.twoClass = response.data
        this.twoClass.unshift({id: "", cat_name: "请选择"})
      })
    },
    /**
     * 查询三级数据
     */
    queryThreeLevelClass(value) {
      this.teacher_catid = '';
      console.log(value);
      cateComList({
        cate_parentid: value,
        cate_secondid: this.ruleForm.teacher_parentid
      }).then(response => {
        console.log(response);
        this.threeClass = response.data
        this.threeClass.unshift({id: "", cat_name: "请选择"})
      })
    },
    /**
     * 获取一级数据
     */
    getOneLevelClass() {
      cateComList({
        cate_parentid: 0,
        cate_secondid: 0
      }).then(response => {
        console.log(response);
        this.oneClass = response.data
        this.oneClass.unshift({id: "", cat_name: "请选择"})
      })
    },
    handleEdit(index, row) {
      console.log(index, row);
    },
    /**
     * 删除
     * @param index
     * @param row
     */
    handleDelete(index, row) {
      this.$confirm('是否要删除该菜单', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        teacherDel(row.id).then(response => {
          this.$message({
            message: '删除成功',
            type: 'success',
            duration: 1000
          });
          this.getTeacherList();
        });
      }).catch(e => e);
    },
    handleSizeChange(val) {
      this.ruleForm.page_size = val;
      this.getTeacherList()
    },
    handleCurrentChange(val) {
      this.ruleForm.page = val;
      this.getTeacherList()
    }
  }
}
</script>

<style lang="scss" scoped>
.block-div {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 20px;
}

.el-select:nth-child(2) {
  margin: 0px 10px;
}

.div-button {
  padding: 12px 0;
  width: 30%;
  margin-left: 0px;
}

.div-button:nth-child(2) {
  margin-left: 20px;
}

.dashboard {
  &-container {
    margin: 0 15px;
    padding: 30px;
    background-color: #fff;
  }

  &-text {
    font-size: 16px;
  }
}
</style>
